package cc.dowell.utils;


import cc.dowell.enums.HttpCodeEnum;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
public class R<T> implements Serializable {

    private Integer code;
    private String msg;
    private T data;


    public R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public R(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static R success() {
        return new R(HttpCodeEnum.SUCCESS.getCode(), HttpCodeEnum.SUCCESS.getMsg());
    }

    public static <T> R<T> error() {
        return new R<T>(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg());
    }

    public static <T> R<T> success(T t) {
        return new R<T>(HttpCodeEnum.SUCCESS.getCode(), HttpCodeEnum.SUCCESS.getMsg(), t);
    }

    public static <T> R<T> error(T t) {
        return new R<T>(HttpCodeEnum.FAIL.getCode(), HttpCodeEnum.FAIL.getMsg(), t);
    }

    public static <T> R<T> error(HttpCodeEnum httpCode) {
        return new R<T>(httpCode.getCode(), httpCode.getMsg());
    }


}

